/*
 * Package il.ac.biu.cs.grossmm.api_tests.data
 * File ActiveDataManagerTest.java
 * Created on Sep 8, 2006
 *
 */
package il.ac.biu.cs.grossmm.junit_api_tests.data;

import static il.ac.biu.cs.grossmm.api.keys.PatternFactory.mandatory;import static il.ac.biu.cs.grossmm.api.keys.PatternFactory.pattern;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
import il.ac.biu.cs.grossmm.api.OperationFailedException;
import il.ac.biu.cs.grossmm.api.flow.ActiveDataManager;
import il.ac.biu.cs.grossmm.api.flow.PersistentPointManager;
import il.ac.biu.cs.grossmm.api.flow.PublicationPoint;
import il.ac.biu.cs.grossmm.api.keys.KeyPattern;
import il.ac.biu.cs.grossmm.api.presence.BaseVocabulary;
import il.ac.biu.cs.grossmm.api.presence.ResourceType;
import il.ac.biu.cs.grossmm.api.presence.Pidf;
import il.ac.biu.cs.grossmm.api.server.Component;
import il.ac.biu.cs.grossmm.api.server.ComponentManager;
import il.ac.biu.cs.grossmm.api.server.ServiceManager;
import il.ac.biu.cs.grossmm.api.sql.SqlDataSource;
import il.ac.biu.cs.grossmm.impl.activeData.ActiveDataManagerImpl;
import il.ac.biu.cs.grossmm.impl.activeData.SqlPointManager;
import il.ac.biu.cs.grossmm.impl.hsqldb.DataSourceAdapter;
import il.ac.biu.cs.grossmm.impl.server.RegistryServer;
import il.ac.biu.cs.grossmm.api.server.Service;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static il.ac.biu.cs.grossmm.api.data.NodeTypeByInterface.*;

public class ActiveDataManagerTest implements Service, Component {
	RegistryServer server = new RegistryServer();
	
	ActiveDataManager adm;
	
	ServiceManager sm;
	
	@Before
	public void startUp() throws Exception {
		server.register(ActiveDataManager.class, new ActiveDataManagerImpl());
		server.register(PersistentPointManager.class, new SqlPointManager());
		server.register(SqlDataSource.class, 
                new DataSourceAdapter("jdbc:hsqldb:file:testpres_db", "sa", ""));
		
		server.register(ActiveDataManagerTest.class, this);
		server.addToStartup(ActiveDataManagerTest.class);
		
		server.start();
	}
	
	@After
	public void tearDown()
	{
		server.stop();
	}

	public void start(ServiceManager serviceManager) throws Exception
	{
		sm = serviceManager;
		System.out.println("TEST STARTED");
	}

	public void stop(Exception exception)
	{
		System.out.println("TEST STOPPED "+exception);
	}

	public void terminated(Object serviceHint, Exception e)
	{
		System.out.println("TERMINATED "+serviceHint+" : "+e);
		sm.terminated(e);
	}

	public void aborted(Object hint, Exception e)
	{
		System.out.println("ABORTED "+hint+" : "+e);
	}

	public void setComponentManager(ComponentManager container) throws Exception
	{
		 adm = (ActiveDataManager) container.getComponent(ActiveDataManager.class);
	}
	
	@Test
	public void startupTest() {
		// ok.
	}
	
	@Test
	public void pubPointOperations1() throws OperationFailedException {
		KeyPattern pattern = pattern(String.class, 
			mandatory(BaseVocabulary.WATCHER, String.class),
			mandatory(BaseVocabulary.RESOURCE_TYPE, ResourceType.PRESENCE)
			);
		PublicationPoint<Pidf> pp = 
			adm.createPublicationPoint(pattern, nodeType(Pidf.class), false, "foo");
		
		PublicationPoint<Pidf> pp2 = adm.getPublicationPoint("foo");
		
		assertEquals(pp, pp2);
		
		pp2 = adm.getPublicationPoint("bar");
		
		assertNull(pp2);
		
		adm.removePublicationPoint("foo");
		
		pp2 = adm.getPublicationPoint("foo");
		
		assertNull(pp2);
	}
	
	@Test
	public void pubPointOperations2() throws OperationFailedException {
		KeyPattern pattern = pattern(String.class, 
			mandatory(BaseVocabulary.WATCHER, String.class),
			mandatory(BaseVocabulary.RESOURCE_TYPE, ResourceType.PRESENCE)
			);
		
		PublicationPoint<Pidf> pp = 
			adm.createPublicationPoint(pattern, nodeType(Pidf.class), true, "foo");
		
		PublicationPoint<Pidf> pp2 = adm.getPublicationPoint("foo");
		
		assertEquals(pp, pp2);
		
		pp2 = adm.getPublicationPoint("bar");
		
		assertNull(pp2);
		
		adm.removePublicationPoint("foo");
		
		pp2 = adm.getPublicationPoint("foo");
		
		assertNull(pp2);
	}
}
